package asteroids.entity.weapon;

import asteroids.engine.GameState;
import asteroids.entity.Ship;

/**
 * Unlike Ships and Equipment, Weapons are not updated every turn. The owning
 * Ship simply calls FIRE(), and the Weapon generates the bullets and adds them
 * to the Ship's manager
 * 
 * @author whitecd1
 * 
 */
public abstract class Weapon
{
    protected Ship ship;
    private double REFIRE_DELAY;
    private double ENERGY_COST;
    protected long lastFireTime;

    public Weapon(Ship ship, double energyCost, double refireDelay)
    {
        this.ship = ship;
        REFIRE_DELAY = refireDelay;
        ENERGY_COST = energyCost;
        lastFireTime = 0;
    }

    public Ship getOwner()
    {
        return ship;
    }

    /**
     * This will determine if a Weapon is able to fire... assuming the subclass
     * maintains the value in lastFireTime or calls manageFireControl() after
     * each successful discharge
     * 
     * @param state
     * @return
     */
    public boolean canFireYet(GameState state)
    {
        return (state.currentGameTime() - lastFireTime >= REFIRE_DELAY && ship.getEnergy() >= ENERGY_COST);
    }

    /**
     * call this after receiving true from canFireYet() - then make killy things
     * 
     * @param state
     */
    public void manageFireControl(GameState state)
    {
        lastFireTime = state.currentGameTime();
        ship.modifyEnergyBy(-ENERGY_COST);
    }

    /**
     * make killy things, most likely. Check any necessary conditions first
     * (energy requirements, projectiles in flight, etc).
     * 
     * @return true if the weapon discharges, false else
     */
    public abstract boolean FIRE(GameState state);

    /**
     * called when the fire button is released. This can be implemented if
     * desired
     */
    public void release()
    {
    };
}
